/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package newsparser.news;

import java.io.Serializable;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.SelectItem;
import newsparser.ConnectorDB;
import org.hibernate.type.descriptor.java.DateTypeDescriptor;
import org.primefaces.event.SelectEvent;

/**
 *
 * @author boyarskiy_km
 */
@ManagedBean
@SessionScoped
public class NewsAction implements Serializable{
    private List<News> news;
    private List<News> newsFilter;
    private List<SelectItem> rubrics;
    private List<String> selectedRubrics;
    private News selectedNews;
    
    private Date filterDateS;
    private Date filterDatePo;
    private Date filterRubric;
    
    
    public News getSelectedNews() {
        return selectedNews;
    }
    public void setSelectedNews(News selectedNews) {
        this.selectedNews = selectedNews;
    }
    public void setSelectedRubrics(List<String> selectedRubrics) {
        this.selectedRubrics = selectedRubrics;
    }
    public List<String> getSelectedRubrics() {
        return selectedRubrics;
    }
    public List<News> getNewsFilter() {
        System.out.println("getNewsFilter");
        return newsFilter;
    }
    public Date getFilterDateS() {
        return filterDateS;
    }
    public void setFilterDateS(Date filterDateS) {
        this.filterDateS = filterDateS;
    }
    public Date getFilterDatePo() {
        return filterDatePo;
    }
    public void setFilterDatePo(Date filterDatePo) {
        this.filterDatePo = filterDatePo;
    }

    
    public List<SelectItem> getRubrics() {
        return rubrics;
    }
    
    public NewsAction() {
        ConnectorDB cdb = new ConnectorDB();
        news = new ArrayList<News>();
        try {
            cdb.rs = cdb.stmt.executeQuery("select * from \"VW_NEWS\""); 
            while (cdb.rs.next()){
                news.add(new News(cdb.rs.getInt("id"), 
                                  cdb.rs.getString("header"),
                                  cdb.rs.getString("rub_name"),
                                  cdb.rs.getInt("rub_id"),
                                  cdb.rs.getString("text"),
                                  cdb.rs.getDate("pubdate")));
            }
            newsFilter = news;
        } catch (SQLException ex) {
            Logger.getLogger(NewsAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        rubrics = new ArrayList<SelectItem>();
        try {
            cdb.rs = cdb.stmt.executeQuery("select * from public.\"VW_RUBRICS\"");
            while (cdb.rs.next()){
                rubrics.add(new SelectItem(cdb.rs.getInt("id"),cdb.rs.getString("name")));
            }
        } catch (SQLException ex) {
            Logger.getLogger(ConnectorDB.class.getName()).log(Level.SEVERE, null, ex);
        } finally{
            cdb.close();
        }
    }
    
    public void filter(SelectEvent event){
        System.out.println("Filter!");
        FacesContext facesContext = FacesContext.getCurrentInstance();  
        SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");  
        facesContext.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected", format.format(filterDateS)));
        
        List<News> news_ = new ArrayList<News>();
       
        
        
        if (filterDateS != null){
            System.out.println(format.format(filterDateS));
            for(int i = 0; i < news.size(); i++){
                if (news.get(i).getDate().before(filterDateS)){
                    news_.add(news.get(i));
                }
            }
            System.out.println(news_.size());
            System.out.println(newsFilter.size());
            newsFilter = news_;
        }else{
            newsFilter = news;
        }
            
    }
    
    
}
